package utils;

import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.Map;

public class BeanUtils {

    public static Map<String, Object> objectToMap(Object obj) {
        return objectToMap(obj, true);
    }

    @SuppressWarnings("unchecked")
    public static Map<String, Object> objectToMap(Object obj, boolean filter) {
        final Map<String, Object> map = JSON.parseObject(JSON.toJSONString(obj), Map.class);
        if (!filter) {
            return map;
        }

        final HashMap<String, Object> result = new HashMap<>();
        map.forEach((key, value) -> {
            if (value != null) {
                result.put(key, value);
            }
        });

        return result;
    }


}
